using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace NpcFramework.Editor
{
    [CustomEditor(typeof(NpcWayPath))]
    public class NpcWayPathEditor : UnityEditor.Editor
    {
        int waitDeleteIndex = -1;
        int waitInsertIndex = -1;
        int waitBranchIndex = -1;

        private Vector2 scrollPos;
        public override void OnInspectorGUI()
        {
            
            base.OnInspectorGUI();

            NpcWayPath npcWayPath = target as NpcWayPath;

            if (GUILayout.Button("刷新"))
            {
                npcWayPath.Refresh();
            }
            
            if (GUILayout.Button("添加主路径点"))
            {
                npcWayPath.AddWayPoint();
            }

//            if (GUILayout.Button("删除选中"))
//            {
//                if (Selection.gameObjects!=null)
//                {
//                    foreach (var o in Selection.gameObjects)
//                    {
//                        var wayPoint = o.GetComponent<NpcWayPoint>();
//                        if (wayPoint!=null)
//                        {
//                            npcWayPath.DeleteIndexPoint(wayPoint);
//                        }
//                    }
//                }
//            }

            if (GUILayout.Button("删除所有"))
            {
                if (npcWayPath.WayPoints.Count > 0)
                {
                    for (int i = npcWayPath.WayPoints.Count - 1; i >= 0; i--)
                    {
                        npcWayPath.DeleteIndexPoint(npcWayPath.WayPoints[i]);
                    }
                }
            }
            
            if (npcWayPath.WayPoints != null)
            {
                scrollPos = EditorGUILayout.BeginScrollView(scrollPos,false,true, GUILayout.Height(400));                
                for (int i = 0; i < npcWayPath.WayPoints.Count; i++)
                {
                    var point = npcWayPath.WayPoints[i];
                    GUILayout.BeginHorizontal();
                    GUILayout.Label("索引:", GUILayout.Width(40));
                    GUILayout.Label(point.Index.ToString(), GUILayout.Width(30));
                    if (GUILayout.Button("删除"))
                    {
                        waitDeleteIndex = i;
                    }

                    if (GUILayout.Button("追加"))
                    {
                        waitInsertIndex = i;
                    }

                    if (GUILayout.Button("添加分支"))
                    {
                        waitBranchIndex = i;
                    }
                    
                    GUILayout.EndHorizontal();
                }
                
                EditorGUILayout.EndScrollView();

                if (waitDeleteIndex != -1)
                {
                    DeleteWaypoint(waitDeleteIndex);
                }

                if (waitInsertIndex!=-1)
                {
                    npcWayPath.InsertWayPoint(waitInsertIndex);      
                    waitInsertIndex = -1;
                }

                if (waitBranchIndex!=-1)
                {
                    npcWayPath.InsertBranchPoint(waitBranchIndex);
                    waitBranchIndex = -1;
                }
            }
            

        }


        private NpcWayPoint GetEndPoint()
        {
            NpcWayPath npcWayPath = target as NpcWayPath;

            var wp = npcWayPath.WayPoints[0];

            while (wp.NextWayPoint && wp.NextWayPoint!=npcWayPath.WayPoints[0])
            {
                wp = wp.NextWayPoint;
            }

            return wp;
        }

        private bool IsBranchPoint(NpcWayPoint point)
        {
            if (point.PrevWayPoint)
            {
                if (point.PrevWayPoint.branchPoints != null)
                {
                    foreach (var bp in point.PrevWayPoint.branchPoints)
                    {
                        if (bp == point)
                        {
                            // 为分支点
                            return true;
                        }
                    }
                }
            }

            return false;
        }
        
        
        private void DeleteWaypoint(int i)
        {
            waitDeleteIndex = -1;
            NpcWayPath npcWayPath = target as NpcWayPath;
            
            var point = npcWayPath.WayPoints[i];

            npcWayPath.DeleteIndexPoint(point);
            
        }
        
    }
}
